// pages/goods/detail/detail.js
import {GoodsModel} from '../../../model/goods'
import {CartModel} from '../../../model/cart'
import { config } from '../../../config'
var goods = new GoodsModel()
var cart = new CartModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    detail:{},
    amount:156.0,
    imgList:[],
    bottomList: ["加入购物车","立即购买"],
    showPop: false,
    statusBarHeight:0
  },

  backNav(){
    wx.navigateBack({
      complete: (res) => {
        console.log(res)
      },
    })
  },

  stopPop(event) {
    if (event.detail.index===0) {
      console.log(event.detail)
      cart.save(event.detail.model.id,event.detail.model.skuModel.id,event.detail.count,).then(
        res=>{
          if (res.code===0) {
            wx.showToast({
              title: '加入购物车成功！',
            })
          }
        }
      )
    }else{
      wx.navigateTo({
        url: '/pages/order/send/send',
        success: function(res) {
          res.eventChannel.emit('detailInfo', event.detail)
        }
      })
    }
  },
  shareAction(){
    console.log("分享")
    wx.showToast({
      title: '分享功能正在开发中！',
      icon: "none"
    })
    // this.setData({
    //   showPop:true
    // })
  },

  

  bottomAction:function(e){
    if (config.isMobile==="1") {
      this.setData({
        showPop:true
      })
    }else{
      wx.showModal({
        content: '请先绑定手机号',
        confirmText: '前往',
        confirmColor: '#F7859C',
        success: res=>{
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
        }
      })
    }
  },

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      statusBarHeight: config.statusBarHeight,
      id:options.id,
    })
    goods.detail(this.data.id).then(
      res=>{
        res.data.goodsSkuNames.forEach(element => {
          element.specification = element.specification.split(';;')
          element.index = null
        });
        res.data.cover = res.data.coverPicture.split(";;")[0]
        console.log(res)
        this.setData({
          detail:res.data,
          imgList:res.data.picture.split(";;")
        })
      }
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let url = encodeURIComponent('/pages/goods/detail/detail?id='+this.data.id);
    return {
      title: this.data.detail.goodsName,
      path: `/pages/home/home?url=${url}`
    }
  }
})